# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : p7.py
# @Time     : 2024/5/8 21:57
# @Author   : Robot-Zsj
"""
description:

数字排列

https://fcqian.blog.csdn.net/article/details/136277460

i:
1,4,8,7

o:
41

"""
nums = list(map(int, input().split(",")))

mapping = {2: 5, 5: 2, 6: 9, 9: 6}


def dfs(vis, path, res):
    if len(path) > 0:
        res.append(int("".join(map(str, path))))
    if len(path) == len(nums):
        return

    for i in range(len(nums)):
        if vis[i]:
            continue

        vis[i] = True

        path.append(nums[i])
        dfs(vis, path, res)
        path.pop()

        # 2,5相互替换
        # 6,9相互替换
        if nums[i] in mapping:
            path.append(mapping[nums[i]])
            dfs(vis, path, res)
            path.pop()
        vis[i] = False


def solution():
    for num in nums:
        if num < 1 or num > 9:
            return -1

    setNums = set(nums)

    if len(setNums) != 4:
        return -1

    if 2 in setNums and 5 in setNums:
        return -1

    if 6 in setNums and 9 in setNums:
        return -1

    vis = [False] * len(nums)
    path = []
    res = []

    dfs(vis, path, res)

    res.sort()

    # N为给出数字中最大的，如果不到这么多数字则给出最后一个即可
    n = min(max(nums), len(res))
    return res[n - 1]


print(solution())
